﻿@{
    ViewBag.Title = "上传图片";
}
<form id="form1" class="layui-form">
    <div class="div-content">
        <div class="layui-form-item">
            <label class="layui-form-label">排序数字</label>
            <div class="layui-input-block">
                <input type="text" id="txtSortId" value="99" class="input txt small" onkeydown="return checkNumber(event);" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标题文字</label>
            <div class="layui-input-block">
                <input type="text" id="txtTitle" class="input txt" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上传图片</label>
            <div class="layui-input-block">
                <input type="text" id="txtImgUrl" class="input txt upload-path" />
                <div class="upload-box choose-pick"><a href="javascript:;" class="webuploader-pick" title="图片空间">选择图片</a></div>
                <div class="upload-box upload-img"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">链接地址</label>
            <div class="layui-input-block">
                <input type="text" id="txtLinkUrl" class="input txt" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片描述</label>
            <div class="layui-input-block">
                <textarea id="txtContent" class="input"></textarea>
            </div>
        </div>
    </div>
</form>
@section Scripts{
    <script type="text/javascript" src="/scripts/libs/js/uploader.js"></script>
    <script type="text/javascript">
        var api = parent.layer.getFrameIndex(window.name);//获取父窗体对象
        layui.use(['element', 'form', 'fileChoose'], function () {
            var fileChoose = layui.fileChoose;
            $('.choose-pick').click(function () {
                var obj = $(this);
                fileChoose.open({
                    fileUrl: '',
                    listUrl: '/admin_ajax/files',
                    num: 1,
                    dialog: {
                        offset: '60px'
                    },
                    onChoose: function (res) {
                        if (res[0].type != "file") {
                            layer.msg('我只让你选择图片格式的文件', { icon: 2, time: 2000 }, function () { });
                            return;
                        }
                        obj.siblings(".upload-path").val(res[0].url);
                        obj.siblings(".layer-photos").show().find("img").attr("src", res[0].url);
                    }
                });
            });
            $(".upload-img").InitUploader({ sendurl: "/upload_ajax/action", swf: "/scripts/libs/module/uploader/uploader.swf" });
        });
        function submitValue(data) {
            //修改状态，赋值给表单
            if ($(data).length > 0) {
                var parentObj = $(data).parent().parent();
                $("#txtImgUrl").val(parentObj.find("input[name='item_imgurl']").val());
                $("#txtTitle").val(parentObj.find("input[name='item_title']").val());
                $("#txtContent").val(parentObj.find("input[name='item_content']").val());
                $("#txtLinkUrl").val(parentObj.find("input[name='item_linkurl']").val());
                $("#txtSortId").val(parentObj.find("input[name='item_sortid']").val());
            }
        }

        //创建选项节点
        function execPicHtml(data) {
            var currDocument = $(document); //当前文档
            if ($("#txtTitle").val() == "") {
                layer.alert('图片文字不可为空！', {
                    closeBtn: 0
                }, function (index) {
                    $("#txtTitle", currDocument).focus();
                    layer.close(index);
                });
                return false;
            }
            if ($("#txtImgUrl").val() == "") {
                layer.alert('图片路径不可为空！', {
                    closeBtn: 0
                }, function (index) {
                    $("#txtImgUrl", currDocument).focus();
                    layer.close(index);
                });
                return false;
            }
            if ($("#txtLinkUrl").val() == "") {
                layer.alert('链接地址不可为空！', {
                    closeBtn: 0
                }, function (index) {
                    $("#txtLinkUrl", currDocument).focus();
                    layer.close(index);
                });
                return false;
            }
            if ($("#txtSortId").val() == "") {
                layer.alert('图片排序不可为空！', {
                    closeBtn: 0
                }, function (index) {
                    $("#txtSortId", currDocument).focus();
                    layer.close(index);
                });
                return false;
            }
            //创建选项节点的HTML
            if ($(data).length > 0) {
                var parentObj = $(data).parent().parent();
                parentObj.find("input[name='item_sortid']").val($("#txtSortId").val());
                parentObj.find("input[name='item_title']").val($("#txtTitle").val());
                parentObj.find("input[name='item_imgurl']").val($("#txtImgUrl").val());
                parentObj.find("input[name='item_linkurl']").val($("#txtLinkUrl").val());
                parentObj.find("input[name='item_content']").val($("#txtContent").val());

                parentObj.find(".item_sortid").html($("#txtSortId").val());
                if ($("#txtImgUrl").val() == "") {
                    parentObj.find(".item_imgurl").html("-");
                } else {
                    parentObj.find(".item_imgurl").html('<img src="' + $("#txtImgUrl").val() + '" width="32" height="32" />');
                }
                parentObj.find(".item_title").html($("#txtTitle").val());
                parentObj.find(".item_linkurl").html($("#txtLinkUrl").val());
                parent.layer.close(api);
            } else {
                var liHtml = '<tr class="td_c">'
                    + '<td><input type="hidden" name="item_id" value="0" />'
                    + '<input type="hidden" name="item_content" value="' + $("#txtContent").val() + '" />'
                    + '<input type="hidden" name="item_imgurl" value="' + $("#txtImgUrl").val() + '" />'
                    + '<span class="item_imgurl img-box"><img src="' + $("#txtImgUrl").val() + '" /></span></td>'
                    + '<td><input type="hidden" name="item_title" value="' + $("#txtTitle").val() + '" />'
                    + '<span class="item_title">' + $("#txtTitle").val() + '</span></td>'
                    + '<td><input type="hidden" name="item_linkurl" value="' + $("#txtLinkUrl").val() + '" />'
                    + '<span class="item_linkurl">' + $("#txtLinkUrl").val() + '</span></td>'
                    + '<td><input type="hidden" name="item_sortid" value="' + $("#txtSortId").val() + '" />'
                    + '<span class="item_sortid">' + $("#txtSortId").val() + '</span></td>'
                    + '<td><a title="编辑" class="img-btn" onclick="showImgDialog(this);"><i class="iconfont icon-edit"></i></a>'
                    + '<a title="删除" class="img-btn" onclick="delItemTr(this);"><i class="iconfont icon-delete"></i></a></td>';
                return liHtml;
            }
            return false;
        }
    </script>
}